home *** CD-ROM | disk | FTP | other *** search
- /***
- * CNBP
- *
- * Name Binding Protocol handler
- *
- * ask this object to handle names over appletalk
- *
- * Copyright © 1992 Bernard Bernstein. All rights reserved.
- ***/
-
-
- #include <TBUtilities.h>
- #include <TCLUtilities.h>
- #include <Global.h>
- #include <CArray.h>
-
- #include "CNBP.h"
-
-
-
- /***
- * INBP
- *
- * Initialize the NBP handler.
- ***/
- void CNBP::INBP(void)
- {
- MPPPBPtr thepb;
-
- if (!IsMPPOpen())
- FailOSErr(MPPOpen());
- if (!IsATPOpen())
- FailOSErr(ATPLoad());
-
- FailOSErr(NBPLoad());
-
- thepb = (MPPPBPtr)NewPtr(sizeof(MPPParamBlock));
- FailNIL(thepb);
-
- nte = (NamesTableEntry*)NewPtr(sizeof(NamesTableEntry));
- FailNIL(nte);
-
- itsMppPb = thepb;
- nameOnTable = false;
- itsNamesSet = false;
- itsSocketSet = false;
-
- }
-
- /***
- * Dispose
- *
- * Kill the structures created by this object.
- ***/
- void CNBP::Dispose(void)
- {
- if (nameOnTable)
- Remove();
-
- ForgetPtr(itsMppPb);
- ForgetPtr(nte);
- inherited::Dispose();
- }
-
-
-
-
- /**********************************************************************
-
- Misc
-
- **********************************************************************/
-
-
- /***
- * SetName
- *
- * Set the name for the nbp object
- ***/
- void CNBP::SetName(Str32 objName, Str32 typeName, Str32 zoneName)
- {
- CopyPString(objName, itsObjName);
- CopyPString(typeName, itsTypeName);
- CopyPString(zoneName, itsZoneName);
- itsNamesSet = true;
- }
-
- /***
- * SetSocket
- *
- * Give a socket to attach to this nbp name
- ***/
- void CNBP::SetSocket(short socket)
- {
- itsSocket = socket;
- itsSocketSet = true;
- }
-
- /***
- * MatchNames
- *
- * Fill the array with the names found
- * matches is an allocate, but not initialized CArray
- *
- * It gets filled with NBPAddrNameRec objects which
- * are defined in CNBP.h, the header for this file
- ***/
- void CNBP::MatchNames(Str32 objMatch, Str32 typeMatch, Str32 zoneMatch,
- CArray *matches)
- {
- short i;
- EntityName anEntity;
- AddrBlock anAddr;
- short numGotten;
- NBPAddrNameRec rec;
- unsigned char *p;
- Ptr nameBuf = nil;
- short bufSize;
- short maxToGet;
-
- ASSERT(matches && member(matches, CArray));
-
- TRY
- {
-
- // setup the buffer to store the matching names
- maxToGet = 50;
- bufSize = maxToGet * (sizeof(EntityName) + sizeof(AddrBlock));
- nameBuf = NewPtr(bufSize);
- FailNIL(nameBuf);
-
-
- // lookup the names and fill the buffer with them
- Lookup(objMatch, typeMatch, zoneMatch, nameBuf, bufSize, maxToGet);
- numGotten = itsMppPb->NBPnumGotten;
-
- if (numGotten > 0)
- {
- for(i=1; i<=itsMppPb->NBPnumGotten; i++)
- {
- NBPExtract(itsMppPb->NBPretBuffPtr, itsMppPb->NBPnumGotten, i,
- &anEntity, &anAddr);
- BlockMove(anEntity.objStr, rec.name, 32);
- BlockMove(anEntity.typeStr, rec.type, 32);
- BlockMove(anEntity.zoneStr, rec.zone, 32);
- rec.addr = anAddr;
- matches->InsertAtIndex( &rec, matches->GetNumItems()+1);
- }
- }
- ForgetPtr(nameBuf);
- }
- CATCH
- {
- ForgetPtr(nameBuf);
- }
- ENDTRY;
-
- }
-
-
- /**********************************************************************
-
- NBP methods
-
- **********************************************************************/
-
-
- /***
- * Register
- *
- * Register this objects name for nbp
- ***/
- void CNBP::Register(void)
- {
- ASSERT(itsSocketSet && (itsSocket != 0));
- ASSERT(itsNamesSet);
-
- NBPSetNTE((Ptr)nte, (Ptr)itsObjName, (Ptr)itsTypeName,
- (Ptr)itsZoneName, itsSocket);
-
- itsMppPb->NBPntQElPtr = (Ptr) nte;
- itsMppPb->NBPinterval = 7;
- itsMppPb->NBPcount = 3;
- itsMppPb->NBPverifyFlag = true;
-
- FailOSErr(PRegisterName(itsMppPb, false));
- nameOnTable = true;
- }
-
- /***
- * Lookup
- *
- * Lookup the object
- ***/
- void CNBP::Lookup(Str32 objMatch, Str32 typeMatch, Str32 zoneMatch,
- void *buffer, short size, short maxToGet)
- {
- EntityPtr mEntity = nil;
-
- mEntity = (EntityPtr)NewPtr(sizeof(EntityName));
- FailNIL(mEntity);
-
- TRY
- {
-
- NBPSetEntity((Ptr)mEntity, (Ptr)objMatch, (Ptr)typeMatch, (Ptr)zoneMatch);
-
- itsMppPb->NBPentityPtr = (Ptr) mEntity;
- itsMppPb->NBPinterval = 5;
- itsMppPb->NBPcount = 5;
- itsMppPb->NBPretBuffPtr = (Ptr) buffer;
- itsMppPb->NBPretBuffSize = size;
- itsMppPb->NBPmaxToGet = maxToGet;
-
- FailOSErr(PLookupName(itsMppPb, false));
-
- ForgetPtr(mEntity);
- }
- CATCH
- {
- ForgetPtr(mEntity);
- }
- ENDTRY;
- }
-
-
-
- /***
- * Remove
- *
- * Remove name from table
- ***/
- void CNBP::Remove(void)
- {
- EntityPtr mEntity = nil;
-
- ASSERT(itsNamesSet);
-
- mEntity = (EntityPtr)NewPtr(sizeof(EntityName));
- FailNIL(mEntity);
-
- NBPSetEntity((Ptr)mEntity, (Ptr)itsObjName, (Ptr)itsTypeName, (Ptr)itsZoneName);
-
- itsMppPb->NBPentityPtr = (Ptr)mEntity;
-
- FailOSErr(PRemoveName(itsMppPb, false));
- ForgetPtr(mEntity);
- nameOnTable = false;
- }
-
-
-
- /***
- * SetSelfSend
- *
- * pass a boolean to set whether we see ourself or not
- * returns what we were before setting it
- ***/
- Boolean CNBP::SetSelfSend(Boolean selfsend)
- {
- MPPPBPtr self;
- Boolean result;
-
- self = (MPPPBPtr)NewPtr(sizeof(MPPParamBlock));
-
- self->SETSELF.newSelfFlag = selfsend ? 1 : 0;
- FailOSErr(PSetSelfSend(self, false));
- result = self->SETSELF.oldSelfFlag != 0;
- ForgetPtr(self);
- }
-